package cn.dida.util;

import java.util.ArrayList;
import java.util.List;

import javax.mail.Address;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

import org.apache.commons.logging.Log;

import cn.dida.core.MyRuntime;
/**
 * 异常处理工具类
 * @author weiwei
 *
 */
public class ExceptionUtil {

	/**
	 * error级别的日志打印，系统报错，如数据库连接不到，空指针等
	 * 
	 * @param loger
	 * @param e
	 */
	public static void logerErrorMes(Log loger, Throwable e) {
		logerErrorMes(loger, "", e);
	}

	/**
	 * error级别的日志打印，系统报错，如数据库连接不到，空指针等
	 * 
	 * @param loger
	 * @param message
	 *            自定义消息
	 * @param e
	 * @author MengTao
	 */
	public static void logerErrorMes(Log loger, String message, Throwable e2) {
		String errormsg = "";
		
		Throwable e = null ;
		
		if(null!=e2){
			e = e2.getCause();
		}
		
		if (null != e) {
			errormsg = e.getMessage() + "\r\n";
			StackTraceElement[] stacka = e.getStackTrace();
			for (int i = 0; i < stacka.length; i++) {
				StackTraceElement stmp = stacka[i];
				errormsg += stmp + "\r\n";
			}
		}
		
		loger.error(message + "\r\n" + errormsg);
		if (MyRuntime.isOnline()) {

			List<Address> list = new ArrayList<Address>();
			// 测试用，后期删掉
			try {
				list.add(new InternetAddress("55801680@qq.com"));
			} catch (AddressException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			Address[] arg1 = list.toArray(new Address[list.size()]);

			 MailUitl.sendMessageByMail("Qrcode_marking_exception", message +"\r\n"+
			 errormsg, arg1);

		}
	}

	/**
	 * 警告级别的日志打印（注意，警告级别的一般都是客户输入造成的错误，程序主动拦截到的）
	 * 
	 * @param loger
	 * @param msg
	 */
	public static void logerWarnMes(Log loger, String msg) {
		loger.warn(msg);
	}

}
